<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Index extends  Base
{

    /**
     * 框架页面
     * @return mixed
     */
    public function index()
    {
        $codepay_config['host'] = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST']; //获取域名
        $servername = get_localdomain_host();
        return $this->fetch('/index',['servername'=>$servername,'admin_allow_title'=>config('admin_allow_title')]);
    }

    /**
     * 首页
     * @return mixed|\think\response\Json
     */
    public function indexPage()
    {
        $codepay_config['host'] = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];
        $servername = get_localdomain_host();
        $todayttime = \fast\Date::unixtime('day', 0);
        $map = [
            'create_time' => ['>= time',$todayttime]
        ];
        $maps = [
            'reg_time' => ['>= time',$todayttime]
        ];
        $art_todaycount = Db::name('article')->where($map)->count();
        $art_count = Db::name('article')->count();
        $vip_todaycount = Db::name('user')->where($maps)->count();
        $vip_count = Db::name('user')->count();
        $wo_count = Db::name('word')->value('word');
        $admin_username = session('username');
        $this->assign('art_todaycount',$art_todaycount);
        $this->assign('art_count',$art_count);
        $this->assign('vip_todaycount',$vip_todaycount);
        $this->assign('vip_count',$vip_count);
        $this->assign('wo_count',$wo_count);
        return $this->fetch('index/index',['servername'=>$servername,'admin_username'=>$admin_username,'admin_allow_title'=>config('admin_allow_title')]);
    }

    /**
     * 今日发布
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function indexTodayAC(){
        $codepay_config['host'] = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST']; //获取域名
        $servername = $codepay_config['host'];
        $todayttime = \fast\Date::unixtime('day', 0);
        $map = [
            'r.create_time' => ['>= time',$todayttime]
        ];
        $field=input('field');//字段
        $order=input('order');//排序方式
        if($field && $order){
            $od="r.".$field." ".$order;
        }else{
            $od="r.create_time desc";
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = input("limit")?input("limit"):10;
        $count = Db::name('article')->alias('r')->where($map)->count();//计算总页面
        $field = "r.title as title,rc.name as name,r.writer as writer,r.views as views";
        $lists = Db::name('article')->alias ('r')->field($field)->join('article_cate rc', 'r.cate_id = rc.id')->where($map)->page($Nowpage, $limits)->order($od)->select();
        for($i=0;$i<count($lists);$i++){
            $lists[$i]['url'] = $servername.'/index/article/detail?id=';
        }
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
    }

    /**
     * 登录日志
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function indexAdminlo(){
        $value = "登录成功";
        $map = [
            'description' => ['like','%'.$value.'%']
        ];
        $field=input('field');//字段
        $order=input('order');//排序方式
        if($field && $order){
            $od=$field." ".$order;
        }else{
            $od="add_time desc";
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = input("limit")?input("limit"):10;// 获取总条数;
        $count = Db::name('log')->where($map)->count();//计算总页面
        $field = "admin_name,ip,ipaddr,add_time";
        $lists = Db::name('log')->field($field)->where($map)->page($Nowpage, $limits)->order($od)->select();
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);

    }

    /**清除缓存
     * @return \think\response\Json
     */
    public function clear() {
        if (delete_dir_file(CACHE_PATH) && delete_dir_file(TEMP_PATH)) {
            writelog('清除缓存成功',200);
            return json(['code' => 200, 'msg' => '清除缓存成功']);
        } else {
            writelog('清除缓存失败',100);
            return json(['code' => 100, 'msg' => '清除缓存失败']);
        }
    }
}